<?php 
require_once "src/apiClient.php";
require_once "src/contrib/apiCalendarService.php";
session_start();
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Add extends CI_Controller {
	
function __construct()
    {
        parent::__construct();
    	$this->load->library('session');
		$this->load->helper('url');
    	$this->load->database();
    	$this->load->model("centermod");
    	
	}
	
	
//checktest
public function checktest()
	{
		//test1
		/*
		$calendarid=$this->centermod->getoption("calendarid");
		 print_r($calendarid);
		*/
	}
	
//bookingform
	public function bookingform()
	{
			if($this->session->userdata("user")==false)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
		
				$this->load->view("bform");
				
			}	
	}
		
	



// actionaddevent
function addappoitment()
{


	 		$calendarid=$this->centermod->getoption("calendarid");
			$address=$this->input->get_post("address");
			$tele=$this->input->get_post("tele");
			$city=$this->input->get_post("city");
			$companyname=$this->input->get_post("companyname");
			$linkgooglep=$this->input->get_post("linkgooglep");
			$Date=$this->input->get_post("Date");
			$strattime=$this->input->get_post("strattime");
			$endtime=$this->input->get_post("endtime");
			$forfait=$this->input->get_post("forfait");
			$Description=$this->input->get_post("Description");
			$decider=$this->input->get_post("decider");
			$d_tele=$this->input->get_post("d_tele");
			$d_email=$this->input->get_post("d_email");
			$sales=$this->input->get_post("sales");

	//create date start and date end
		$date1 = new DateTime($Date);
		$date1->setTimezone(new DateTimeZone('America/Montreal'));
		$date1->setTime($strattime, 00);
		$astart= date_format($date1, 'Y-m-d\TH:i:sP');

		//end
		$date2 = new DateTime($Date);
		$date2->setTimezone(new DateTimeZone('America/Montreal'));
		$date2->setTime($endtime, 00);
		$aend= date_format($date2, 'Y-m-d\TH:i:sP');	
	//----------------------	

		
		
	$str_summary=$companyname." ".$address." ".$forfait;


	$client = new apiClient();
$client->setApplicationName("Google Calendar PHP Starter Application");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
 $client->setClientId('770938131025.apps.googleusercontent.com');
$client->setClientSecret('bOih7XMjWC6E5M18eaGSV8EI');
 $client->setRedirectUri('http://localhost:8888/pshowmanager/index.php/add/sendevent');

$client->setDeveloperKey('AI39si6a39ey9Zl_elSjD3eTiJV7Aiav2FK3luNQweZGdvLckqPGINxRF349spYyrzESSINdA3kOQgQknnROREMKhzOtNg0Zvw');
$client->setUseObjects(true);
$cal = new apiCalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
	 header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
 $calList = $cal->calendarList->listCalendarList();
	//print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

	
	//$events = $cal->events->listEvents('xiaomingenvtest@gmail.com');
echo '---------------get liste by id---------------<br/>';
	
$calendarListEntry = $cal->calendarList->get($calendarid);

echo $calendarListEntry->getSummary();


//-------insert event--------	
$event = new Event();
$event->setSummary($str_summary);
$event->setDescription($Description	);	

$event->setLocation($address);
$start = new EventDateTime();
$start->setDateTime($astart);
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime($aend);
$event->setEnd($end);
$attendee1 = new EventAttendee();
$attendee1->setEmail('wangxiaoming@hotmail');
$attendees = array($attendee1 );
$event->attendees = $attendees;
$createdEvent = $cal->events->insert($calendarid, $event);
echo $createdEvent->getId();	


	
// update event
	
	
	
	$_SESSION['token'] = $client->getAccessToken();
	
  
  



} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
	

}//addappoitment

	



}